<?php

Class Player extends CI_Model
{

	private static $instance;

	private $name;
	private $id;
	private $gameId;
	
	public function __construct() {
		parent::__construct();
		$this->id = 0;
		$this->name = "";
		$gameId = $this->session->userdata('gameId');
	}
	
	public function acceptCards($playerId){
		//TODO: GET ALL ACTIVE CARDS ON TURNS
		$CI =& get_instance();
        $CI->load->model('turns');
        $CI->load->model('talons');
		$turnCards = $CI->turns->getActiveTurnCards();
		return $CI->talons->updateReplenishee_idToPlayer($turnCards,$playerId);
	}
	
	public function endAttack($playerId){
		//TODO: disable all active turn cards and active talon card
		$CI =& get_instance();
        $CI->load->model('turns');
        $CI->turns->endAttack($playerId);
	}
	
	
	public function attack($playerId,$talonId){
		$CI =& get_instance();
        $CI->load->model('turns');
        $CI->load->model('talons');
        $turnCard = $CI->turns->insertAttackTurn($playerId, $talonId);

		$CI->talons->revealedCard($talonId);
		$this->session->set_userdata('current_turnId',$playerId==1 ? 2 : 1);
		return $turnCard;
//		if($playerId==1){// player 2 replenishes first 
//			$this->replenishCards(2);
//			$this->replenishCards(1);
//		}else{// player 1 replenishes first 
//			$this->replenishCards(1);
//			$this->replenishCards(2);
//		}
//		return $CI->talons->getTalonCardByGameIdAndPlayerIdNotRevealed($playerId);$turn
	}
	
	public function defend($playerId,$talonId){
		$CI =& get_instance();
        $CI->load->model('turns');//insertAttackTurn
        $CI->load->model('talons');
        $turnCard = $CI->turns->insertDefendTurn($playerId, $talonId);
		$CI->talons->revealedCard($talonId);
		return $turnCard;
//		$this->session->set_userdata('current_turnId',$playerId==1 ? 2 : 1);
//		if($playerId==1){// player 2 replenishes first 
//			$this->replenishCards(2);
//			$this->replenishCards(1);
//		}else{// player 1 replenishes first 
//			$this->replenishCards(1);
//			$this->replenishCards(2);
//		}
//		return $CI->talons->getTalonCardByGameIdAndPlayerIdNotRevealed($playerId);
	}
	
	
	
	private function counCardsOnHand($playerId){
		$CI =& get_instance();
        $CI->load->model('talons');
		return count($CI->talons->getTalonCardByGameIdAndPlayerId($playerId));
	}
	
	public function replenishePlayer($playerId){
		$cardsOnHand = $this->counCardsOnHand($playerId);
		if($cardsOnHand<6 ){
			$CI =& get_instance();
			$CI->load->model('talons');
			return $CI->talons->replenishPlayerByOne($playerId);
		}
		//return;
	}
	
	public function replenishCards($playerId){									// triggered when player (1)end an attack
		$cardsOnHand = $this->counCardsOnHand($playerId);						// (2)accept cards
		if($cardsOnHand<6 & $cardsOnHand!=null){
			$CI =& get_instance();
        	$CI->load->model('talons');
			$CI->talons->replenishPlayer($playerId, 6-$cardsOnHand);
		}
		return $this->getPlayerCards($playerId);
	}

	public function getPlayerName(){
		return $this->name;
	}
	public function getPlayerId(){
		return $this->id;
	}
	public function createPlaters(){
		$this->id  	  = null; // id is auto incremented
		$this->name   = $_POST['title']; 
		$this->db->insert('player', $this);
    }
	public function getPlayers(){
		$query = $this->db->get("player");
		return $query->result();
	}
	
	public function getNewSetPlayerCardOnhand($playerId){
		$CI =& get_instance();
        $CI->load->model('talons');
		return $CI->talons->createNewSetPlayerCardOnhand($playerId);					// returns list of new cards for player from talons cards
	}
	
	public function getPlayerCards($playerId){
		$CI =& get_instance();
        $CI->load->model('talons');
		return $CI->talons->getTalonCardByGameIdAndPlayerId($playerId);
	}
	
}
?>